/**
  ETFDAQ Project
  \class TAThread
  \brief a general testing thread class to prepare for the DAQ thread
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2024-08-20
  \date 2024-08-20 last modified

  \copyright Copyright (c) 2024 IMP-CAS with LGPLv3 LICENSE
*/

#include <iostream>
#include <unistd.h>
#include "TAThread.h"

using std::cout;
using std::endl;

TAThread::TAThread(TAGDAQ *gdaq) : QThread((QObject *)gdaq), fGDAQ(gdaq){}

TAThread::~TAThread(){}

void TAThread::run(){
  static int cnt = 0;
  EDAQSTA sta = daqStatus();
  while(1){
    sta = daqStatus();
    switch(sta){
      case EDAQSTA::START:
      case EDAQSTA::RESUME:
        cout << __TIME__ << ":" << (cnt++ % 100) << endl; sleep(1); break; // test
      case EDAQSTA::PAUSE:
      case EDAQSTA::STOP:
      case EDAQSTA::EXIT: break;
      default: break;
    } // end switch
    if(EDAQSTA::EXIT == sta || EDAQSTA::STOP == sta) break;
  } // end while
  exec(); // takes signals to exit
} // end member function run

void TAThread::updateStatus(const EDAQSTA &sta){
  QMutexLocker lock(&fMutex);
  fDaqSta = sta;
} // end membre function updateStatus

EDAQSTA TAThread::daqStatus(){
  QMutexLocker lock(&fMutex);
  return fDaqSta;
} // end member function daqStatus
